﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using SE560Multimedia.src;

namespace SE560Multimedia.pages
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        SessionManager sessionManager;

        protected void Page_Load(object sender, EventArgs e)
        {
            sessionManager = new SessionManager(Session);
            LabelOperationResult.Text = "";
        }

        protected void AddMediaButton_Click(object sender, EventArgs e)
        {
            if (FileUploadMedia.PostedFile != null) {
                try {
                    String mediaFileName = FileUploadMedia.PostedFile.FileName;
                    mediaFileName = mediaFileName.Substring(mediaFileName.LastIndexOf("\\")+1);

                    FileUploadMedia.PostedFile.SaveAs(
                        ConfigurationSettings.AppSettings["MediaPath"] + mediaFileName);
                
                    ProjectDataSetTableAdapters.mediaTableAdapter mediaTableAdapter =
                        new ProjectDataSetTableAdapters.mediaTableAdapter();
                    mediaTableAdapter.Insert(sessionManager.SessionLoggedUserId,
                        mediaFileName, TextBoxMediaName.Text, 0,
                        TextBoxAlbumName.Text, TextBoxArtistName.Text, CheckBoxPrivateToOwner.Checked ? (ulong)1 : (ulong)0);

                    if (CheckBoxAddToPlaylist.Checked)
                    {
                        int lastInsertedMediaId = (int)mediaTableAdapter.GetLastInsertId();
                        ProjectDataSetTableAdapters.playlistitemTableAdapter plylistItemAdapter =
                            new ProjectDataSetTableAdapters.playlistitemTableAdapter();
                        plylistItemAdapter.Insert(lastInsertedMediaId, sessionManager.SessionLoggedPlaylistID);
                    }

                    LabelOperationResult.Text = "Media Uploaded Succesfully";
                }
                catch (Exception) {
                }
            }

        }
    }
}
